package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysMenuVo;
import com.ruoyi.system.domain.bo.SysMenuBo;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜单权限
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    private final ISysMenuService iSysMenuService;

    /**
     * 查询菜单权限列表
     */
    @SaCheckPermission("system:menu:list")
    @GetMapping("/list")
    public TableDataInfo<SysMenuVo> list(SysMenuBo bo, PageQuery pageQuery) {
        return iSysMenuService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出菜单权限列表
     */
    @SaCheckPermission("system:menu:export")
    @Log(title = "菜单权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMenuBo bo, HttpServletResponse response) {
        List<SysMenuVo> list = iSysMenuService.queryList(bo);
        ExcelUtil.exportExcel(list, "菜单权限", SysMenuVo.class, response);
    }

    /**
     * 获取菜单权限详细信息
     *
     * @param menuId 主键
     */
    @SaCheckPermission("system:menu:query")
    @GetMapping("/{menuId}")
    public R<SysMenuVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long menuId) {
        return R.ok(iSysMenuService.queryById(menuId));
    }

    /**
     * 新增菜单权限
     */
    @SaCheckPermission("system:menu:add")
    @Log(title = "菜单权限", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMenuBo bo) {
        return toAjax(iSysMenuService.insertByBo(bo));
    }

    /**
     * 修改菜单权限
     */
    @SaCheckPermission("system:menu:edit")
    @Log(title = "菜单权限", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysMenuBo bo) {
        return toAjax(iSysMenuService.updateByBo(bo));
    }

    /**
     * 删除菜单权限
     *
     * @param menuIds 主键串
     */
    @SaCheckPermission("system:menu:remove")
    @Log(title = "菜单权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] menuIds) {
        return toAjax(iSysMenuService.deleteWithValidByIds(Arrays.asList(menuIds), true));
    }
}
